package com.whiteape.book.presenter;


import com.whiteape.book.contract.HomepageContract;
import com.whiteape.book.core.manager.MediaStorageManager;
import com.whiteape.book.core.manager.WebMediaManager;
import com.whiteape.book.model.bo.CategoryDetailBo;
import com.whiteape.book.model.dto.SiteSourceBean;
import com.whiteape.book.model.dto.SiteSourceCategoryBean;
import com.whiteape.book.ui.base.RxPresenter;
import com.whiteape.book.utils.DaoDbHelper;
import com.whiteape.book.utils.RxUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class HomepagePresenter extends RxPresenter<HomepageContract.View> implements HomepageContract.Presenter{

    Disposable mLastDisposable;
    @Override
    public void refreshHomepageData(SiteSourceBean siteSourceBean, List<SiteSourceCategoryBean> categoryBeanList) {
        if (categoryBeanList == null || categoryBeanList.isEmpty()) {
            return;
        }
//        addDisposable(WebMediaManager.getInstance().getHomepageData(siteSourceBean)
//                .compose(RxUtils::toSimpleSingle)
//                .subscribe(beanList -> {
//                    if (mView != null) {
//                        mView.finishRefresh(beanList);
//                    }
//                }, throwable -> {
//                    if (mView != null) {
//                        mView.finishError(throwable);
//                    }
//                }));

        //每次最多跑一个网源的数据，下个来了，上一个要清理掉（可能还在跑）
        if (mLastDisposable != null) {
            mLastDisposable.dispose();
        }

        //先检查本地是不是有数据

        List<SiteSourceCategoryBean> filteredCategoryBeanList = new ArrayList<>();
        for (SiteSourceCategoryBean categoryBean : categoryBeanList) {
            List<CategoryDetailBo> categoryDetailBoList = new ArrayList<>();
            categoryBean.__setDaoSession(DaoDbHelper.getInstance().getSession());
            categoryBean.resetCategoryDetailBoList();
            List<CategoryDetailBo> singleCategoryDetailBoList = categoryBean.getCategoryDetailBoList();
            if (singleCategoryDetailBoList == null || singleCategoryDetailBoList.isEmpty()) {
                filteredCategoryBeanList.add(categoryBean);
            }else {
                for (CategoryDetailBo detailBo : singleCategoryDetailBoList) {
                    detailBo.setCategoryBean(categoryBean);
                    detailBo.setSiteSourceBean(siteSourceBean);
                }
                categoryDetailBoList.addAll(singleCategoryDetailBoList);
                if (categoryDetailBoList.size() > 0) {
                    if (mView != null) {
                        mView.finishRefresh(categoryDetailBoList);
                    }
                }
            }
        }


        //看是不是还有哪些本地没有存储的
        if (filteredCategoryBeanList.size() == 0) {
            return;
        }
        WebMediaManager.getInstance()
                .getHomepageData(siteSourceBean, filteredCategoryBeanList)
                .compose(RxUtils::toSimpleSingle)
                .subscribe(new Observer<List<CategoryDetailBo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mLastDisposable = d;

                    }

                    @Override
                    public void onNext(List<CategoryDetailBo> categoryDetailBos) {

                        MediaStorageManager.getInstance().saveCategoryBoListInRx(categoryDetailBos);
                        if (mView != null) {
                            mView.finishRefresh(categoryDetailBos);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

//                        if (mView != null) {
//                            mView.finishError(e);
//                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}

